package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.TrainAuditManagerDetail;
import com.xmy.cultivate.service.ITrainAuditManagerDetailService;
import com.xmy.cultivate.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 审核主管设置对应的详情 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-11-07
 */
@RestController
@RequestMapping("/api/trainAuditManagerDetail")
public class TrainAuditManagerDetailController {

    @Autowired
    private ITrainAuditManagerDetailService iTrainAuditManagerDetailService;

    @Autowired
    private IdWorker idWorker;

    @GetMapping("/page")
    public Result page(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "auditManagerSetId") String auditManagerSetId,  //train_audit_manager_set表id
            @RequestParam(value = "schoolId", required = false) String schoolId,// 校区id
            @RequestParam(value = "name", required = false) String name
    ) {
        IPage<TrainAuditManagerDetail> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<TrainAuditManagerDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit_manager_set_id", auditManagerSetId);
        queryWrapper.eq(StringUtils.isNotEmpty(schoolId), "school_id", schoolId);
        queryWrapper.like(StringUtils.isNotEmpty(name), "name", name);
        iPage = iTrainAuditManagerDetailService.page(iPage, queryWrapper);
        for (TrainAuditManagerDetail record : iPage.getRecords()) {
            String subjectsIdStr = record.getSubjectsIdStr();
            StringBuilder subjectsNameStr = new StringBuilder();
            if (StringUtils.isNotBlank(subjectsIdStr)) {
                String[] subjectsIdArray = subjectsIdStr.split(",");
                for (int i = 0; i < subjectsIdArray.length; i++) {
                    String subjectsId = subjectsIdArray[i];
                    subjectsNameStr.append(CommonUtil.getSubjectsNameForId(Integer.valueOf(subjectsId)));
                    if (i < subjectsIdArray.length - 1) {
                        subjectsNameStr.append(",");
                    }
                }
            }
            record.setSubjectsNameStr(subjectsNameStr.toString());


            StringBuilder yearClassNameStr = new StringBuilder();
            String yearClassIdStr = record.getYearClassIdStr();
            if (StringUtils.isNotBlank(yearClassIdStr)) {
                String[] yearClassIdArray = yearClassIdStr.split(",");
                for (int i = 0; i < yearClassIdArray.length; i++) {
                    String yearClassId = yearClassIdArray[i];
                    yearClassNameStr.append(CommonUtil.getYearClassNameForId(Integer.valueOf(yearClassId)));
                    if (i < yearClassIdArray.length - 1) {
                        yearClassNameStr.append(",");
                    }
                }
            }
            record.setYearClassNameStr(String.valueOf(yearClassNameStr));
        }
        return new Result(Code.GET_OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), Code.GET_OK_MSG);
    }

    @PostMapping("/save")
    public Result save(@RequestBody TrainAuditManagerDetail trainAuditManagerDetail) {
        long count = iTrainAuditManagerDetailService.count(new QueryWrapper<>(trainAuditManagerDetail));
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "已存在，请勿重复添加");
        }
        trainAuditManagerDetail.setId(idWorker.nextId());
        boolean save = iTrainAuditManagerDetailService.save(trainAuditManagerDetail);
        return new Result(save ? Code.SAVE_OK : Code.SAVE_ERROR, null, save ? Code.SAVE_OK_MSG : Code.SAVE_ERROR_MSG);
    }

    @PutMapping("/update")
    public Result update(@RequestBody TrainAuditManagerDetail trainAuditManagerDetail) {
        QueryWrapper<TrainAuditManagerDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", trainAuditManagerDetail.getId());
        queryWrapper.eq("audit_manager_set_id", trainAuditManagerDetail.getAuditManagerSetId());
        queryWrapper.eq("subjects_id_str", trainAuditManagerDetail.getSubjectsIdStr());
        queryWrapper.eq("year_class_id_str", trainAuditManagerDetail.getYearClassIdStr());
        long count = iTrainAuditManagerDetailService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "已存在，请勿重复编辑");
        }
        boolean update = iTrainAuditManagerDetailService.updateById(trainAuditManagerDetail);
        return new Result(update ? Code.UPDATE_OK : Code.UPDATE_ERROR, null, update ? Code.UPDATE_OK_MSG : Code.UPDATE_ERROR_MSG);
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Map map) {
        List<Long> ids = (List<Long>) map.get("ids");
        boolean b = iTrainAuditManagerDetailService.removeBatchByIds(ids);
        return new Result(b ? Code.DELETE_OK : Code.DELETE_ERROR, null, b ? Code.DELETE_OK_MSG : Code.DELETE_ERROR_MSG);
    }
}
